<!-- 我的查询 -->

<template>
  <WebSearch noSearch>
    <template #btn>
      <div>
        <ParamRadio bind="type" :dict="DICT.TYPE" isBtn @change="changeType" />

        <ParamWeekRange v-if="param.type === 2" bind="begin, end" noClear @change="changeWeek" />
        <ParamMonth v-else-if="param.type === 3" bind="begin" noClear @change="changeMonth" />

        <BtnDownload @click="startDownload" />
      </div>
    </template>
  </WebSearch>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import { getXData } from '../store/state'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MySearch',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixLoad()],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 切换类型
     */
    changeType() {
      const { param } = this
      const { type, begin, end } = param

      // 切换为月 (该周跨月时，设置为周日那天所在的月份，以免来回切换周和月时，不断和往前翻月)
      if (type === 3) {
        const [beginMonth, endMonth] = [CORE.dateStr(begin).slice(0, 7), CORE.dateStr(end).slice(0, 7)]
        if (beginMonth !== endMonth) param.begin = CORE.timeNum(endMonth)

        this.changeMonth(param.begin)
      } else {
        this.changeWeek()
      }
    },

    /**
     * 切换星期
     */
    changeWeek() {
      const { param } = this
      const { begin } = param
      param.xData = getXData(begin, 7)
    },

    /**
     * 切换月份
     */
    changeMonth(begin) {
      const { param } = this
      const days = CORE.monthDays(begin)

      param.end = begin + 86400000 * days - 1000
      param.xData = getXData(begin, days)
    },
  },
}
</script>
